/*
* wpxmlrpc.h - This file is the part of the wp-notifier.(code name "luan luan")
* Copyright (C) 2010 Dean Lee <deanlee3@gmail.com>, http://www.deanlee.cn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* Acknowledgements:
*   <> Many thanks to all of you, who have encouraged me to update my articles
*     and code, and who sent in bug reports and fixes.
* 
*/
#ifndef WPXMLRPC_H
#define WPXMLRPC_H
#include "./qxmlrpc/client/src/xmlrpcclient.h"
#include <QDateTime>
#include "configdlg.h"
#include "comment.h"
class BlogData;
class wpCommand : public QObject
{
	Q_OBJECT
public:
	enum Type{GetCommentsCount, GetComments, GetComment, BlogInfo, NewComment, DeleteComment, EditComment} ;
	wpCommand(wpCommand::Type type, const Blog& blog, QObject* parent, bool autoDelete = true);
	wpCommand(wpCommand::Type type, const QString& webaddress, int blogid, const QString& username,
		const QString& password, QObject* parent, bool autoDelete = true);
	const QByteArray& rawResponse()const{return m_client->rawResponse();}
	void executeCommand(const QString &method, const QVariantList &params);
	const QString& error()const{return m_error;}
	Type type(){return m_type;}
	int blogId()const{return m_blogid;}
	virtual ~wpCommand();
	static void shutDownAllCommands();
signals:
	void finished(bool);
protected :
	virtual bool onDataReady( const QVariant &response ) = 0;
	virtual void onError(const QString&);
private slots:
	void _onDataReady( const QVariant &response, const QString& err );
	void _onError(const QString&);
protected:
	void init(wpCommand::Type type, const QString& webaddress, int blogid,
		const QString& username, const QString& password, bool autoDelete);
	bool m_autoDelete;
	Type m_type;
	QString m_error;
	QUrl m_url;
	int m_blogid;
	QString m_username;
	QString m_password;
private:
	XmlRpcClient *m_client;
	static QList<wpCommand*> s_runningCommands;
};

class wpGetUsersBlogs : public wpCommand
{
	Q_OBJECT
public:
	wpGetUsersBlogs (const QString& webaddress, const QString& username, const QString& password, QObject* parent);
	~wpGetUsersBlogs ();
	void execute();
	QList<Blog> m_blogs;
protected:
	bool onDataReady( const QVariant &response);
};

class wpGetCommentCount : public wpCommand
{
	Q_OBJECT
public:
	wpGetCommentCount(const Blog& blog, QObject* parent);
	~wpGetCommentCount();
	void execute(int postid);
	bool onDataReady( const QVariant &response );
	int approved;
	int awaiting_moderation;
	int spam;
	int total_comments ;

};

class wpGetComments : public wpCommand
{
	Q_OBJECT
public:
	enum OperationType{Refresh, FetchMore};
	wpGetComments(const Blog& blog, OperationType type, QObject* parent);
	~wpGetComments();
	void execute(int postid, const QString status, int offset, int number);
	bool onDataReady( const QVariant &response);
	QList<Comment> m_comments;
	int m_nOffset;
	QString m_status;
	OperationType m_operationType;

};


class wpGetAllComments : public QObject
{
	Q_OBJECT
public:
	enum OperationType{Refresh, FetchMore};
	wpGetAllComments(const Blog* blog, QObject* parent);
	~wpGetAllComments();
	void execute(int postid, int number);
	//bool onDataReady( const QVariant &response);
	QList<Comment> m_comments;
	int m_nOffset;
	QString m_status;
	int m_count;
	int m_approve;
	int m_spam;
	int m_hold;
	QString m_error;
	const Blog* m_blog;
signals:
	void finished(bool);
protected slots:
	void xmlrpcFinished(bool);

};



class wpDeleteComment : public wpCommand
{
	Q_OBJECT
public:
	wpDeleteComment(const Blog& blog, QObject* parent);
	~wpDeleteComment();
	void execute(int commentid);
	bool onDataReady( const QVariant &response);
};

class wpNewComment : public wpCommand
{
	Q_OBJECT
public:
	wpNewComment(const Blog& blog, QObject* parent);
	~wpNewComment();
	void execute(int postid, int comment_parent, const QString& content);
	bool onDataReady( const QVariant &response);
	int m_commentid;
};

class wpGetComment : public wpCommand
{
	Q_OBJECT
public:
	wpGetComment(const Blog& blog, QObject* parent);
	~wpGetComment();
	void execute(int commentid);
	bool onDataReady( const QVariant &response);
	Comment m_comment;
};

class wpEditComment : public wpCommand
{
	Q_OBJECT
public:
	wpEditComment(const Blog& blog, QObject* parent);
	~wpEditComment();
	void execute(int comment_id, const QString& status, const QString& content);
	bool onDataReady( const QVariant &response);
};
#endif